package com.zxn.rv;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.lzf.easyfloat.EasyFloat;
import com.lzf.easyfloat.anim.AppFloatDefaultAnimator;
import com.lzf.easyfloat.anim.DefaultAnimator;
import com.lzf.easyfloat.enums.ShowPattern;
import com.lzf.easyfloat.enums.SidePattern;
import com.lzf.easyfloat.permission.PermissionUtils;
import com.zxn.rv.event.UserBean;
import com.zxn.rv.utils.UserInfoManager;
import com.zxn.rv.utils.ViewCenterUtils;
import com.zxn.rv.weight.DragFloatActionButton;

import org.greenrobot.eventbus.EventBus;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    private DragFloatActionButton dragFloatActionButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loadPermissions();
        initView();
        //getSupportFragmentManager().beginTransaction().replace(R.id.fl_container, RvFrag.newInstance()).commitAllowingStateLoss();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    private void initView() {
        imageView = findViewById(R.id.tv_go);
        dragFloatActionButton = findViewById(R.id.to_live_drag);

        String username = "张三";
        String password = "123456";
        String phone = "18709287807";
        String nickname = "张老三";
        //点击跳转到下一个页面
        imageView.setOnClickListener(v -> {
            int[] viewCenter = ViewCenterUtils.getViewCenter(imageView);
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            intent.putExtra("x", viewCenter[0]);
            intent.putExtra("y", viewCenter[1]);
            UserInfoManager.saveSportKM("10km");
            startActivity(intent);
            //事件发布者发布事件
            EventBus.getDefault().postSticky(new UserBean(username,password,phone,nickname));
        });
        dragFloatActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"点击拖拽",Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 请求悬浮窗权限
     */
    private void loadPermissions() {
        if (PermissionUtils.checkPermission(this)) {
            String liveId = "100086";
            //初始化悬浮窗
            initFlowWindow(liveId);
        }else {
            final AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setMessage("使用浮窗功能，需要您授权悬浮窗权限。");
            alert.setNegativeButton("取消", ((dialog, which) -> dialog.dismiss()));
            alert.setPositiveButton("去开启", (dialog, which) -> {
                String liveId = "100086";
                initFlowWindow(liveId);
                dialog.dismiss();
            });
            alert.show();
        }
    }

    /**
     * 初始化悬浮窗布局
     * @param liveId
     */
    private void initFlowWindow(String liveId) {
            if (!TextUtils.isEmpty(liveId)) {
                EasyFloat.with(this)
                        // 设置浮窗xml布局文件
                        .setLayout(R.layout.layout_float_window, view -> {
                            // view就是我们传入的浮窗xml布局
                            view.findViewById(R.id.go_to_image).setOnClickListener(view1 -> {
                                //回直播
                                startActivity(new Intent(MainActivity.this,LiveActivity.class));
                                //隐藏悬浮框
                                EasyFloat.hideAppFloat("live");
                                EasyFloat.dismissAppFloat("live");
                            });
                        })
                        // 设置浮窗显示类型，默认只在当前Activity显示，可选一直显示、仅前台显示
                        .setShowPattern(ShowPattern.FOREGROUND)
                        // 设置吸附方式，共15种模式，详情参考SidePattern
                        .setSidePattern(SidePattern.RESULT_HORIZONTAL)
                        // 设置浮窗的标签，用于区分多个浮窗
                        .setTag("live")
                        // 设置浮窗是否可拖拽
                        .setDragEnable(true)
                        // 设置浮窗的对齐方式和坐标偏移量
                        .setGravity(Gravity.END | Gravity.CENTER_VERTICAL, 0, 200)
                        // 设置宽高是否充满父布局，直接在xml设置match_parent属性无效
                        .setMatchParent(false, false)
                        // 设置Activity浮窗的出入动画，可自定义，实现相应接口即可（策略模式），无需动画直接设置为null
                        .setAnimator(new DefaultAnimator())
                        // 设置系统浮窗的出入动画，使用同上
                        .setAppFloatAnimator(new AppFloatDefaultAnimator()).show();
                EasyFloat.showAppFloat("live");
            } else {
                EasyFloat.hide();
                EasyFloat.hideAppFloat("live");
            }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
